package com.example.text.ui.mall.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.text.base.BaseViewModel
import com.example.text.base.state.Default
import com.example.text.base.state.Empty
import com.example.text.base.state.Failed
import com.example.text.base.state.Loading
import com.example.text.base.state.RequestEvent
import com.example.text.base.state.Success
import com.example.text.ui.mall.model.AdInfoModel
import com.example.text.ui.mall.repository.MallRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class MallViewModel @Inject constructor(private val mallRepository: MallRepository): BaseViewModel(){
    private val _mallAdInfoStateFlow  by lazy {
        MutableStateFlow<RequestEvent>(Default)
    }
    val mallAdInfoStateFlow = _mallAdInfoStateFlow.asStateFlow()

    private val _mallAdInfoLiveData by lazy{
        MutableLiveData<AdInfoModel>()
    }
    val mallAdInfoLiveData: LiveData<AdInfoModel> = _mallAdInfoLiveData

    fun cmallAdInfo(){
        execute(
            action = mallRepository.cmallAdInfo(),
            onStart = {
                //开始请求
                _mallAdInfoStateFlow.value = Loading
            },
            onError = {
                //请求失败
                _mallAdInfoStateFlow.value = Failed(it)
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _mallAdInfoStateFlow.value = Empty
                }else{
                    _mallAdInfoStateFlow.value = Success

                  //  _mallAdInfoLiveData.value = it.data
                }

            }
        )
    }
}